package com.fw.ui.view.settings.usercontrol.merchantuser.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.ui.bao.MessageBAO;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractListener;
import com.fw.ui.view.settings.usercontrol.merchantuser.CreateMerchantUserView;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;

@Component
@Scope(SCOPE_PROTOTYPE)
public class CreateMerchantUserMerchantOptionGroupValueChangeListener extends AbstractListener implements ValueChangeListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MessageBAO messageBAO;

    @Override
    public void valueChange(ValueChangeEvent event) {
	CreateMerchantUserView createMerchantUserView = (CreateMerchantUserView) this.getBasedComponent();
	String selected = (String) event.getProperty().getValue();
	if (createMerchantUserView.getMerchantOption().equals(selected)) {
	    createMerchantUserView.getCbbMerchant().setReadOnly(false);
	    createMerchantUserView.getCbbService().setValue(null);
	    createMerchantUserView.getCbbService().setReadOnly(true);
	    createMerchantUserView.getCbbParent().setValue(null);
	    createMerchantUserView.getCbbParent().setReadOnly(true);
	    createMerchantUserView.getCbbMerchant().setRequired(true);
	    createMerchantUserView
		    .getCbbMerchant()
		    .setRequiredError(
			    messageBAO
				    .getMessage("com.fw.ui.view.settings.usercontrol.merchantuser.CreateMerchantUserView.message.merchantID.mandatory"));
	    createMerchantUserView.getCbbParent().setRequired(false);
	    createMerchantUserView.getCbbParent().setRequiredError("");
	} else if (createMerchantUserView.getParentOption().equals(selected)) {
	    createMerchantUserView.getCbbMerchant().setValue(null);
	    createMerchantUserView.getCbbService().setValue(null);
	    createMerchantUserView.getCbbMerchant().setReadOnly(true);
	    createMerchantUserView.getCbbService().setReadOnly(true);
	    createMerchantUserView.getCbbParent().setReadOnly(false);
	    createMerchantUserView.getCbbParent().setRequired(true);
	    createMerchantUserView
		    .getCbbParent()
		    .setRequiredError(
			    messageBAO
				    .getMessage("com.fw.ui.view.settings.usercontrol.merchantuser.CreateMerchantUserView.message.parentID.mandatory"));
	    createMerchantUserView.getCbbMerchant().setRequired(false);
	    createMerchantUserView.getCbbMerchant().setRequiredError("");
	}
    }
}
